//
//  HNSH_ApplyRefundFooter.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_ApplyRefundFooter.h"

@interface HNSH_ApplyRefundFooter ()

@property (nonatomic, strong)UILabel * titleLabel;
/* bgView */
@property (nonatomic, strong) UIView *bgView;


@property (nonatomic, strong)UILabel * desLabel;
@property (nonatomic, strong)UIButton * sureBtn;

/* account_type 0 无现金退款 可以不传 1：支付宝 2：银行卡*/
@property (nonatomic, assign) NSInteger account_type;
/* 姓名 */
@property (nonatomic, copy) NSString *name;
/* 账号 */
@property (nonatomic, copy) NSString *account;
/* 银行卡开户行 */
@property (nonatomic, copy) NSString *additional;
@end

@implementation HNSH_ApplyRefundFooter

//MARK: Cycle Methods
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)configAccount:(BOOL)show_account account_list:(NSArray *)account_list {
    if (show_account) {
        self.account_type = 1;
        [self configZFBBgView];
    } else {
        self.account_type = 0;
        self.bgView.hidden = self.titleLabel.hidden = YES;
        [self.desLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(16);
        }];
    }
}

- (void)addAllLayout{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.height.mas_equalTo(22);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(221);
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.desLabel.mas_bottom).offset(60);
        make.height.mas_equalTo(50);
    }];
}



- (void)showSelectType {
    WeakSelf(weakSelf);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"支付宝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.account_type = 1;
        [weakSelf configZFBBgView];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"银行卡" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.account_type = 2;
        [weakSelf configBankBgView];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"支付宝" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:alert animated:YES completion:nil];
}

- (void)configZFBBgView {
    
    self.bgView.frame = CGRectMake(20, 46, kScreenWidth-40, 150);
    
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
    [self.desLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(221);
    }];
    
    UILabel *firstLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 16, 200, 20)];
    firstLeftLabel.text = @"请选择到账类型";
    firstLeftLabel.textColor = [UIColor appGrayLabelColor];
    firstLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:firstLeftLabel];
    
    UILabel *firstRightLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bgView.width-233, 16, 200, 20)];
    firstRightLabel.text = @"支付宝";
    firstRightLabel.textColor = [UIColor appGrayLabelColor];
    firstRightLabel.font = [UIFont systemFontOfSize:14];
    firstRightLabel.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:firstRightLabel];
    
    UIImageView *firstRightImgV = [[UIImageView alloc]initWithFrame:CGRectMake(self.bgView.width-27, 23, 12, 7)];
    firstRightImgV.image = [UIImage imageNamed:@"mine_order_apply_refund_show"];
    [self.bgView addSubview:firstRightImgV];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.bgView.width, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bgView addSubview:line];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, self.bgView.width, 50);
    button.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:button];
    WeakSelf(weakSelf);
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf showSelectType];
    }];
    
    UILabel *secondLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 66, 200, 20)];
    secondLeftLabel.text = @"姓名：";
    secondLeftLabel.textColor = [UIColor appGrayLabelColor];
    secondLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:secondLeftLabel];
    
    UITextField *secondRightField = [[UITextField alloc]initWithFrame:CGRectMake(self.bgView.width-233, 66, 200, 20)];
    secondRightField.placeholder = @"请输入收款人姓名";
    secondRightField.textColor = [UIColor appBlackLabelColor];
    secondRightField.font = [UIFont systemFontOfSize:14];
    secondRightField.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:secondRightField];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 100, self.bgView.width, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bgView addSubview:line1];
    
    UILabel *thirdLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 116, 200, 20)];
    thirdLeftLabel.text = @"账号：";
    thirdLeftLabel.textColor = [UIColor appGrayLabelColor];
    thirdLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:thirdLeftLabel];
    
    UITextField *thirdRightField = [[UITextField alloc]initWithFrame:CGRectMake(self.bgView.width-233, 116, 200, 20)];
    thirdRightField.placeholder = @"请输入收款支付宝账号";
    thirdRightField.textColor = [UIColor appBlackLabelColor];
    thirdRightField.font = [UIFont systemFontOfSize:14];
    thirdRightField.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:thirdRightField];
    
    [[secondRightField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        weakSelf.name = field.text;
    }];
    [[thirdRightField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        weakSelf.account = field.text;
    }];
}


- (void)configBankBgView {
    self.bgView.frame = CGRectMake(20, 46, kScreenWidth-40, 200);
    
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
    [self.desLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(272);
    }];
    
    UILabel *firstLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 16, 200, 20)];
    firstLeftLabel.text = @"请选择到账类型";
    firstLeftLabel.textColor = [UIColor appGrayLabelColor];
    firstLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:firstLeftLabel];
    
    UILabel *firstRightLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bgView.width-233, 16, 200, 20)];
    firstRightLabel.text = @"银行卡";
    firstRightLabel.textColor = [UIColor appGrayLabelColor];
    firstRightLabel.font = [UIFont systemFontOfSize:14];
    firstRightLabel.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:firstRightLabel];
    
    UIImageView *firstRightImgV = [[UIImageView alloc]initWithFrame:CGRectMake(self.bgView.width-27, 23, 12, 7)];
    firstRightImgV.image = [UIImage imageNamed:@"mine_order_apply_refund_show"];
    [self.bgView addSubview:firstRightImgV];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.bgView.width, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bgView addSubview:line];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, self.bgView.width, 50);
    button.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:button];
    WeakSelf(weakSelf);
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf showSelectType];
    }];
    
    UILabel *secondLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 66, 200, 20)];
    secondLeftLabel.text = @"姓名：";
    secondLeftLabel.textColor = [UIColor appGrayLabelColor];
    secondLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:secondLeftLabel];
    
    UITextField *secondRightField = [[UITextField alloc]initWithFrame:CGRectMake(self.bgView.width-233, 66, 200, 20)];
    secondRightField.placeholder = @"请输入收款人姓名";
    secondRightField.textColor = [UIColor appBlackLabelColor];
    secondRightField.font = [UIFont systemFontOfSize:14];
    secondRightField.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:secondRightField];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 100, self.bgView.width, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bgView addSubview:line1];
    
    UILabel *thirdLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 116, 200, 20)];
    thirdLeftLabel.text = @"银行卡号：";
    thirdLeftLabel.textColor = [UIColor appGrayLabelColor];
    thirdLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:thirdLeftLabel];
    
    UITextField *thirdRightField = [[UITextField alloc]initWithFrame:CGRectMake(self.bgView.width-233, 116, 200, 20)];
    thirdRightField.placeholder = @"请输入收款银行卡账号";
    thirdRightField.textColor = [UIColor appBlackLabelColor];
    thirdRightField.font = [UIFont systemFontOfSize:14];
    thirdRightField.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:thirdRightField];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 150, self.bgView.width, 1)];
    line2.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.bgView addSubview:line2];
    
    UILabel *thourLeftLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 166, 200, 20)];
    thourLeftLabel.text = @"开户行：";
    thourLeftLabel.textColor = [UIColor appGrayLabelColor];
    thourLeftLabel.font = [UIFont systemFontOfSize:14];
    [self.bgView addSubview:thourLeftLabel];
    
    UITextField *thourRightField = [[UITextField alloc]initWithFrame:CGRectMake(self.bgView.width-233, 166, 200, 20)];
    thourRightField.placeholder = @"请输入银行卡所属开户行";
    thourRightField.textColor = [UIColor appBlackLabelColor];
    thourRightField.font = [UIFont systemFontOfSize:14];
    thourRightField.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:thourRightField];
    
    [[secondRightField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        weakSelf.name = field.text;
    }];
    [[thirdRightField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        weakSelf.account = field.text;
    }];
    [[thourRightField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        weakSelf.additional = field.text;
    }];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self addSubview:self.titleLabel];
    [self addSubview:self.bgView];
    
    [self addSubview:self.desLabel];
    [self addSubview:self.sureBtn];
}

- (void)sureBtnAction{
    //无退款方式
    if (self.account_type == 0) {
        if (self.commitFeedbackBlock) {
            self.commitFeedbackBlock(0,@"",@"",@"");
        }
        return;
    }
    if (![self.name isNotBlank]) {
        [kSTWindow makeToast:@"请输入姓名"];
        return;
    }
    if (![self.account isNotBlank]) {
        [kSTWindow makeToast:@"请输入收款账号"];
        return;
    }
    if (![self.additional isNotBlank] && self.account_type == 2) {
        [kSTWindow makeToast:@"请输入开户行"];
        return;
    }
    if (self.commitFeedbackBlock) {
        self.commitFeedbackBlock(self.account_type,self.name,self.account,self.additional);
    }
}

//MARK: Setter Methods
- (void)setIsRefund:(BOOL)isRefund{
    _isRefund = isRefund;
    
    if (isRefund) {
        self.desLabel.text = @"温馨提示：\n1.商品的退货审核结果可以在“我的-退款/售后”中查看审核结果 \n2.提交申请后，客服人员可能与您进行电话沟通，请保持手机畅通 \n3.退货成功后，退款将会原路返回至您的账户中，具体金额需要跟客服进行协商处理。";
    } else {
        self.desLabel.text = @"温馨提示： \n1.商品的退货审核结果可以在“我的-退款/售后”中查看审核结果\n2.提交申请后，客服人员可能与您进行电话沟通，请保持手机畅通\n3.退货成功后，退款将会原路返回至您的账户中，具体金额需要跟客服进行协商处理。";
    }
}

//MARK: Getter Methods
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"退款账户";
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
    }
    return _titleLabel;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(20, 46, kScreenWidth-40, 150)];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        _bgView.layer.cornerRadius = 4.0f;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UILabel *)desLabel{
    if (!_desLabel) {
        _desLabel = [UILabel new];
        _desLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _desLabel.font = [UIFont systemFontOfSize:12];
        _desLabel.numberOfLines = 0;
        _desLabel.text = @"温馨提示： \n1.商品的退货审核结果可以在“我的-退款/售后”中查看审核结果\n2.提交申请后，客服人员可能与您进行电话沟通，请保持手机畅通\n3.退货成功后，退款将会原路返回至您的账户中，具体金额需要跟客服进行协商处理。";
    }
    return _desLabel;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _sureBtn.layer.cornerRadius = 25;
        _sureBtn.layer.masksToBounds = YES;
        _sureBtn.backgroundColor = [UIColor appGreenColor];
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

@end
